package com.cg.design.graphicdesignpattern.command;

import com.cg.design.graphicdesignpattern.command.command.MacroCommand;
import com.cg.design.graphicdesignpattern.command.drawer.ColorCommand;
import com.cg.design.graphicdesignpattern.command.drawer.DrawCanvas;
import com.cg.design.graphicdesignpattern.command.drawer.DrawCommand;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @program: LeetCode->CommandTest
 * @description: Command命令模式测试类
 * @author: cg
 * @create: 2022-04-05 19:34
 **/
public class CommandTest extends JFrame {

    /**
     * 绘制的历史记录
     */
    private final MacroCommand history = new MacroCommand();

    /**
     * 绘制区域
     */
    private final DrawCanvas canvas = new DrawCanvas(400, 400, history);

    /**
     * 删除按钮
     */
    private final JButton clearButton = new JButton("clear");
    /**
     * 红色按钮
     */
    private final JButton redButton = new JButton("red");
    /**
     * 绿色按钮
     */
    private final JButton greenButton = new JButton("green");
    /**
     * 蓝色按钮
     */
    private final JButton blueButton = new JButton("blue");
    /**
     * 撤销按钮
     */
    private final JButton undoButton = new JButton("undo");

    /**
     * 构造函数
     *
     * @param title
     */
    public CommandTest(String title) {
        super(title);

        canvas.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                DrawCommand command = new DrawCommand(canvas, e.getPoint());
                history.append(command);
                command.execute();
            }
        });
        clearButton.addActionListener(e -> {
            if (e.getSource() == clearButton) {
                history.clear();
                canvas.init();
                canvas.repaint();
            }
        });
        redButton.addActionListener(e -> {
            if (e.getSource() == redButton) {
                ColorCommand command = new ColorCommand(canvas, Color.red);
                history.append(command);
                command.execute();
            }
        });
        greenButton.addActionListener(e -> {
            if (e.getSource() == greenButton) {
                ColorCommand command = new ColorCommand(canvas, Color.green);
                history.append(command);
                command.execute();
            }
        });
        blueButton.addActionListener(e -> {
            if (e.getSource() == blueButton) {
                ColorCommand command = new ColorCommand(canvas, Color.blue);
                history.append(command);
                command.execute();
            }
        });
        undoButton.addActionListener(e -> {
            if (e.getSource() == undoButton) {
                history.undo();
                canvas.repaint();
            }
        });

        Box buttonBox = new Box(BoxLayout.X_AXIS);
        buttonBox.add(clearButton);
        buttonBox.add(redButton);
        buttonBox.add(greenButton);
        buttonBox.add(blueButton);
        buttonBox.add(undoButton);
        Box mainBox = new Box(BoxLayout.Y_AXIS);
        mainBox.add(buttonBox);
        mainBox.add(canvas);
        getContentPane().add(mainBox);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        show();
    }

    public static void main(String[] args) {
        new CommandTest("Command pattern Test");
    }

}
